package Opeartion;

import book.Book;
import book.Booklist;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 */
public class AddBook implements Operation {
    @Override
    public void work(Booklist booklist) {
        System.out.println("新增图书......");
        Scanner scanner =new Scanner(System.in);
        System.out.println("请输入新增书的名字：");
        String name=scanner.nextLine();
        System.out.println("请输入新增书的作者：");
        String author=scanner.nextLine();
        System.out.println("请输入新增书的价格：");
        int price =scanner.nextInt();
        scanner.nextLine();
        System.out.println("请输入新增书的类型：");
        String type =scanner.nextLine();
        int currentSize=booklist.getBrrownum();
        for (int i = 0; i < currentSize; i++) {
            if (booklist.getBook(i).getName().equals(name)){
                System.out.println("已有这本书了，不能插入！");
                return;
            }
        }
        Book newbook =new Book(name,author,price,type);
        booklist.setBook(newbook,currentSize);
        booklist.setBrrownum(currentSize+1);
        System.out.println("新增成功！");
    }
}
